// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0


import { MaterialPalette } from "styling.slint";

component ScrollBar inherits Rectangle {
    in-out property <bool> horizontal;
    in-out property <length> maximum;
    in-out property <length> page-size;
    // this is always negative and bigger than  -maximum
    in-out property <length> value;
    in-out property <bool> enabled <=> touch-area.enabled;
    in property <ScrollBarPolicy> policy: ScrollBarPolicy.as-needed;

    callback scrolled();

    states [
        disabled when !touch-area.enabled : {
            background.border-color: MaterialPalette.control-foreground;
            handle.opacity: 0.12;
        }
        hover when touch-area.has-hover : {
            state-layer.opacity: 0.08;
        }
        pressed when touch-area.has-hover : {
            state-layer.opacity: 0.12;
        }
    ]

    visible: (self.policy == ScrollBarPolicy.always-on) || (self.policy == ScrollBarPolicy.as-needed && self.maximum > 0);
    clip: true;

    state-layer := Rectangle {
        width: 100%;
        height: 100%;
        background: MaterialPalette.accent-background;
        border-radius: 4px;
        opacity: 0;
        visible: handle.width > 0 && handle.height > 0;

        animate opacity { duration: 250ms; easing: ease; }
    }

    handle := Rectangle {
        x: !root.horizontal ? 0phx : (root.width - handle.width) * (-root.value / root.maximum);
        y: root.horizontal ? 0phx : (root.height - handle.height) * (-root.value / root.maximum);
        width: !root.horizontal ? parent.width : root.maximum <= 0phx ? 0phx : max(min(32px, root.width), parent.width * max(root.page-size / (root.maximum + root.page-size)));
        height: root.horizontal ? parent.height : root.maximum <= 0phx ? 0phx : max(min(32px, root.height), parent.height * (root.page-size / (root.maximum + root.page-size)));

        background := Rectangle {
            width: 100%;
            height: 100%;
            border-radius: 4px;
            border-color: MaterialPalette.border;
            border-width: 1px;
        }
    }

    touch-area := TouchArea {
        property <{pressed-value: length, maximum: length, x:length, y: length}> saved-values;

        width: parent.width;
        height: parent.height;

        function update-saved-values() {
            self.saved-values = { pressed-value: -root.value, maximum: root.maximum, x: self.mouse-x, y: self.mouse-y };
        }

        pointer-event(event) => {
            if (event.button == PointerEventButton.left && event.kind == PointerEventKind.down) {
                self.update-saved-values();
            }
        }

        moved => {
            if (self.enabled && self.pressed) {
                // Update reference points when the size of the viewport changes to
                // avoid 'jumping' during scrolling.
                // This happens when height estimate of a ListView changes after
                // new items are loaded.
                if (self.saved-values.maximum != root.maximum) {
                    self.update-saved-values();
                }

                root.value = -max(0px, min(root.maximum, self.saved-values.pressed-value + (
                    root.horizontal ? (self.mouse-x - self.saved-values.x) * (root.maximum / (root.width - handle.width))
                               : (self.mouse-y - self.saved-values.y) * (root.maximum / (root.height - handle.height))
                )));
                root.scrolled();
            }
        }

        scroll-event(event) => {
            if (root.horizontal && event.delta-x != 0) {
                root.value = max(-root.maximum, min(0px, root.value + event.delta-x));
                return accept;
            } else if (!root.horizontal && event.delta-y != 0) {
                root.value = max(-root.maximum, min(0px, root.value + event.delta-y));
                return accept;
            }
            reject
        }
    }
}

// Scrollview contains a viewport that is bigger than the view and can be scrolled.
export component ScrollView {
    in property <bool> enabled: true;
    out property <length> visible-width <=> flickable.width;
    out property <length> visible-height <=> flickable.height;
    in-out property <bool> has-focus;
    in-out property <length> viewport-width <=> flickable.viewport-width;
    in-out property <length> viewport-height <=> flickable.viewport-height;
    in-out property <length> viewport-x <=> flickable.viewport-x;
    in-out property <length> viewport-y <=> flickable.viewport-y;
    in property <ScrollBarPolicy> vertical-scrollbar-policy <=> vertical-bar.policy;
    in property <ScrollBarPolicy> horizontal-scrollbar-policy <=> horizontal-bar.policy;
    in property <bool> mouse-drag-pan-enabled <=> flickable.interactive;

    callback scrolled <=> flickable.flicked;

    min-height: 50px;
    min-width: 50px;
    horizontal-stretch: 1;
    vertical-stretch: 1;
    preferred-height: 100%;
    preferred-width: 100%;

    flickable := Flickable {
        x: 0;
        y: 0;
        viewport-y <=> vertical-bar.value;
        viewport-x <=> horizontal-bar.value;
        width: parent.width - vertical-bar.width - 4px;
        height: parent.height - horizontal-bar.height - 4px;

        @children
    }

    vertical-bar := ScrollBar {
        width: 8px;
        x: flickable.width + flickable.x;
        y: flickable.y;
        height: flickable.height;
        horizontal: false;
        maximum: flickable.viewport-height - flickable.height;
        page-size: flickable.height;
        enabled: root.enabled;

        scrolled => {root.scrolled()}
    }

    horizontal-bar := ScrollBar {
        height: 8px;
        y: flickable.height + flickable.y;
        x: flickable.x;
        width: flickable.width;
        horizontal: true;
        maximum: flickable.viewport-width - flickable.width;
        page-size: flickable.width;
        enabled: root.enabled;

        scrolled => {root.scrolled()}
    }
}
